﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class Mem_Extend
    {
        public int Add(Chain.Model.Mem_Extend model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Mem_Extend(");
            builder.Append("MemID,StartTime,EndTime,isonline,Score,Money,Wxpocket,isdel)");
            builder.Append(" values (");
            builder.Append("@MemID,@StartTime,@EndTime,@isonline,@Score,@Money,@Wxpocket,0)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] {
                new SqlParameter("@MemID", SqlDbType.Int,4),
                new SqlParameter("@StartTime", SqlDbType.DateTime,4),
                new SqlParameter("@EndTime", SqlDbType.DateTime,4),
                new SqlParameter("@isonline", SqlDbType.Int,4),
                new SqlParameter("@Score", SqlDbType.Int,4),
                new SqlParameter("@Money", SqlDbType.Decimal,4),
                new SqlParameter("@Wxpocket", SqlDbType.Decimal,4)
             };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.StartTime;
            cmdParms[2].Value = model.EndTime;
            cmdParms[3].Value = model.isonline;
            cmdParms[4].Value = model.Score;
            cmdParms[5].Value = model.Money;
            cmdParms[6].Value = model.Wxpocket;


            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool UpdateClose(int MemID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Mem_Extend set ");
            builder.Append("isonline=0");          
            builder.Append(" where MemID=@MemID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int,4) };
            cmdParms[0].Value = MemID;
        
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }


        public bool IsExist(int MemID) {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Mem_Extend");
            builder.Append(" where MemID=@MemID and isonline=1 and EndTime>getdate()");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }


        public DataTable GetList(int MemID) {

            StringBuilder builder = new StringBuilder();
            builder.Append("select * from Mem_Extend where MemID=@MemID and isonline=1 and EndTime>getdate() and StartTime<GETDATE()");

            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;

            DataTable dt = DbHelperSQL.Query(builder.ToString(),cmdParms).Tables[0];

            return dt;
        }
    }
}
